<?php


namespace app\controller;

use app\model\Blogs;
use think\facade\Request;
use think\facade\Session;

class Comment
{
    // 评论博客
    public function comment(Request $request ,Blogs $blogs ,$id)
    {
        // 获取当前博客
        $blog = $blogs->find($id);

        // 保存评论
        $comment = $blog->comments()->save([
            'user_id' => Session::get('uid'),
            'content' => $request::param('content')
        ]);

        if ($comment){
            $resDate = [
                'avatar_url' => Session::get('avatar'),
                'user_name' => Session::get('name'),
                'content' => $request::param('content'),
            ];

            return json(['code' => 200, 'date' => $resDate, 'msg' => '评论成功']);
        } else {
            return json(['code' => 400, '评论失败']);
        }
    }
}